package com.wavercaz.freeforum.controllers;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wavercaz.freeforum.pojo.CommonSearchParam;
import com.wavercaz.freeforum.pojo.UserFocusInfo;
import com.wavercaz.freeforum.services.UserFocusInfoService;
import com.wavercaz.freeforum.utils.JWTUtil;
import com.wavercaz.freeforum.utils.QueryUtil;
import com.wavercaz.freeforum.utils.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
public class UserFocusInfoController {
    @Autowired
    private  UserFocusInfoService userFocusInfoService;

    // 增
    @PutMapping("/userFocusInfo")
    public ResponseResult save(@RequestHeader("Authorization") String token,@RequestBody UserFocusInfo userFocusInfo) {
        userFocusInfo.setUserId(Integer.parseInt(JWTUtil.verifyToken(token).getClaim("id").asString()));
        boolean result = userFocusInfoService.save(userFocusInfo);
        if(!result) return new ResponseResult(HttpStatus.NO_CONTENT.value(), "关注失败");
        else return new ResponseResult(HttpStatus.OK.value(), "关注成功");
    }

    // 删
    @DeleteMapping("/userFocusInfo")
    public ResponseResult delete(@RequestBody List<Integer> ids) {
        boolean result = userFocusInfoService.removeByIds(ids);
        if(!result) return new ResponseResult(HttpStatus.NO_CONTENT.value(), "删除失败");
        else return new ResponseResult(HttpStatus.OK.value(), "删除成功");
    }

    // 改
    @PostMapping("/userFocusInfo")
    public ResponseResult update(@RequestHeader("Authorization") String token,@RequestBody UserFocusInfo userFocusInfo) {
        userFocusInfo.setUserId(Integer.parseInt(JWTUtil.verifyToken(token).getClaim("id").asString()));
        QueryWrapper<UserFocusInfo> queryWrapper = new QueryWrapper<UserFocusInfo>()
                .eq("user_id",userFocusInfo.getUserId())
                .eq("be_focused_user_id",userFocusInfo.getBeFocusedUserId());
        UserFocusInfo data = userFocusInfoService.getOne(queryWrapper);
        data.setIsFocus(userFocusInfo.getIsFocus());
        boolean result = userFocusInfoService.updateById(data);
        if(!result) return new ResponseResult(HttpStatus.NO_CONTENT.value(), "取关失败");
        else return new ResponseResult(HttpStatus.OK.value(), data.getIsFocus() == 0?"取关成功":"关注成功");
    }

    // 查
    @GetMapping("/userFocusInfo/{id}")
    public ResponseResult get(@PathVariable Integer id ) {
        UserFocusInfo userFocusInfo = userFocusInfoService.getById(id);
        if(userFocusInfo == null) return new ResponseResult(HttpStatus.NO_CONTENT.value(), "无此相关信息");
        else return new ResponseResult(HttpStatus.OK.value(), userFocusInfo);
    }

    // 列表
    @PostMapping("/userFocusInfoList")
    public ResponseResult list(@RequestHeader(value = "Authorization", required = false) String token, @RequestBody(required = false) CommonSearchParam commonSearchParam) {
        Object data = null;
        if (token == null) {
            return new ResponseResult(HttpStatus.OK.value());
        }
        QueryWrapper<UserFocusInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id",Integer.parseInt(JWTUtil.verifyToken(token).getClaim("id").asString()));
        if(commonSearchParam != null) {
            queryWrapper = QueryUtil.getQueryWrapper(commonSearchParam.getSearches());
            queryWrapper.eq("user_id",Integer.parseInt(JWTUtil.verifyToken(token).getClaim("id").asString()));
            if(commonSearchParam.getPageNum()!=null && commonSearchParam.getPageSize()!=null)
                data= userFocusInfoService.page(new Page<>(commonSearchParam.getPageNum(), commonSearchParam.getPageSize()), queryWrapper);
            else data= userFocusInfoService.list(queryWrapper);
        }
        else data= userFocusInfoService.list(queryWrapper);
        return new ResponseResult(HttpStatus.OK.value(), data);
    }
}
